<?php
/***
 * 颜色后台Service
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */


namespace App\Services\ColourService;
use App\Services\ImgServices\ImgTool;
use Illuminate\Support\Facades\Log;
use Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Lang;
use Illuminate\Http\Request;
use App\Services\Tool\GeneralTool;
class BackstageColourService
{
    /**
     * 添加验证
     * @param array $colour
     * @return mixed
     */
    public function addValidator(array $colour)
    {
        $v = [
            'colour_name' => 'required|max:20',
            'colour_code' => 'max:7|min:7',
            'description' => 'max:200',
        ];
        return  Validator::make(
            $colour,$v

        );
    }

    /**
     * 修改验证
     * @param array $colour
     * @return mixed
     */
    public function updateValidator(array $colour)
    {
        $v = [
            'colour_name' => 'required|max:20',
            'colour_code' => 'max:7|min:7',
            'description' => 'max:200',
        ];
        return  Validator::make(
            $colour,$v

        );
    }
    /**
     * 添加颜色
     * @param $colour
     */
    public function addColour(array $colour)
    {
        $colour['state'] = 1;
        $colour['created_at'] = time();
        $colour['updated_at'] = time();
        $num = DB::table('colours')->insert($colour);
        return $num;
    }

    /**
     * 通过颜色名称获取颜色
     * @param $name
     * @return mixed
     */
    public function getColourForName($name){
        $colour =  DB::table('colours')->where(array('colour_name'=>$name,'state'=>1))->first();
        return $colour;
    }

    /**
     * 除当前颜色外是否有重复颜色名称
     * @param string $name
     * @return mixed
     */
    public function getColourForNameIsNotCurrent($colour_id,$name){
        $colour =  DB::table('colours')->where(array('colour_name'=>$name,'state'=>1))->where('id','<>',$colour_id)->first();
        return $colour;
    }

    /**
     * 获取颜色列表
     * @param Request $request
     */
    public function getColourList(Request $request)
    {
        $search = $request->input('search','');
        $colours = DB::table('colours')->where('state',1)->where('colour_name','like','%'.$search.'%')
            ->orderBy('updated_at','DESC')->paginate(10);
        foreach ($colours as $colour){
            $colour->colour_image=ImgTool::getImgUrlByImgPath($colour->colour_image);
        }
        $colours->appends(['search' => $search])->render();
        $colours->search = $search;
        return $colours;
    }

    /***
     * 获取所有颜色
     * @param Request $request
     * @return mixed
     */
    public function getAllColourList(Request $request)
    {
        $search = $request->input('search','');
        $colours = DB::table('colours')->where('state',1)->where('colour_name','like','%'.$search.'%')->get();
        return $colours;
    }
    /***
     * 删除某一颜色
     * @param $colour_id
     * @return mixed
     */
    public function deleteColour($colour_id){
        $num =  DB::table("colours")->where('id', $colour_id) ->update(['state' => 0,'updated_at'=>time()]);
        return $num;
    }

    /**
     * 修改颜色
     * @param array $colour
     * @return mixed
     */
    public function updateColour(array $colour){
        $colour['updated_at'] = time();
        $num =  DB::table("colours")->where('id', $colour['id'])->update($colour);
        return $num;
    }

    /**
     * 根据ID获取未删除的颜色
     * @param $colour_id
     * @return mixed
     */
    public function getColourForIds($colour_ids)
    {
        $colour_id_list='';
        foreach ($colour_ids as $colour_id){
            $colour=DB::table("colours")->where(array('id'=>$colour_id,'state'=>1))->first();
            if(empty($colour)){
                $colour_id_list.=$colour_id.',';
            }
        }
        return $colour_id_list;
    }
    /**
     * 根据ID获取未删除的尺码
     * @param $size_id
     * @return mixed
     */
    public function getColourForId($colour_id)
    {
        return DB::table("colours")->where(array('id'=>$colour_id,'state'=>1))->first();
    }
    /**
     * 删除某一产品下所有
     * @param $product_id
     * @return mixed
     */
    public function deleteProductColourState($product_id)
    {
        return  DB::table('product_colours')->where('product_id',$product_id)->update(['state' => 2]);
    }

    /***
     * 删除某一产品下所有模板
     * @param $product_id
     * @return mixed
     */
    public function deleteProductTempState($product_id)
    {
        return  DB::table('product_templates')->where('product_id',$product_id)->update(['state' => 2]);
    }
    /***
     * 删除某一产品对应下的颜色
     * @param $prod_colour_id
     * @return mixed
     */
    public function deleteProductColourById($prod_colour_id)
    {
        return  DB::table('product_colours')->where('id',$prod_colour_id)->update(['state' => 2]);
    }

    /***
     * 删除对应产品模板
     * @param $prod_template_id
     * @return mixed
     */
    public function deleteProductTemplateById($prod_template_id)
    {
        return  DB::table('product_templates')->where('id',$prod_template_id)->update(['state' => 2]);
    }

    /***
     * 更新模板排序
     * @param $temp_id
     * @param $sort
     * @return mixed
     */
    public function tempUpdateSort($temp_id,$sort)
    {
        return  DB::table('product_templates')->where('id',$temp_id)
                                                 ->update(['sort' => $sort,'updated_at'=>time()]);
    }
    /**
     * 保存产品颜色
     * @param $colour_id
     * @param $product_id
     */
    public function saveProductColour($colour_id,$product_temp_id)
    {
        $productColour = $this->getProductTempColour($colour_id,$product_temp_id);
        if($productColour==null)
        {
            $product_colour['colour_id']=$colour_id;
            $product_colour['product_temp_id']=$product_temp_id;
            $product_colour['state'] = 1;
            $product_colour['created_at'] = time();
            $product_colour['updated_at'] = time();
            DB::table('product_temp_colours')->insert($product_colour);
        }else{
            $this->updateProductColourState($colour_id,$product_temp_id);
        }

    }

    /***
     * 保存产品模板
     * @param $colour_id
     * @param $product_id
     */
    public function saveProductTemplate($colour_id,$product_temp_id)
    {
        $productColour = $this->getProductTempColour($colour_id,$product_temp_id);
        if($productColour==null)
        {
            $product_colour['colour_id']=$colour_id;
            $product_colour['product_temp_id']=$product_temp_id;
            $product_colour['state'] = 1;
            $product_colour['created_at'] = time();
            $product_colour['updated_at'] = time();
            DB::table('product_temp_colours')->insert($product_colour);
        }else{
            $this->updateProductColourState($colour_id,$product_temp_id);
        }

    }
    /***
     * 获取关系 包括删除的
     * @param $colour_id
     * @param $product_id
     * @return mixed
     */
    public function getProductTempColour($colour_id,$product_temp_id){
        return  DB::table('product_temp_colours')->where(array('colour_id'=>$colour_id,'product_temp_id'=>$product_temp_id))->first();
    }

    /***
     * 恢复已删除
     * @param $colour_id
     * @param $product_id
     * @return mixed
     */
    public function updateProductColourState($colour_id,$product_temp_id)
    {
        $res=DB::table('product_temp_colours')->where(array('colour_id'=>$colour_id,'product_temp_id'=>$product_temp_id))
                 ->update(['state' => 1,'updated_at'=>time()]);
        return $res;
    }

    /***
     * 获取产品模板下的所有颜色
     * @param $product_temp_id
     * @return array
     */
    public function getProductColoursByProductTempId($product_temp_id){
        $product_temp_id=trim($product_temp_id);
        if(empty($product_temp_id)){
            return array();
        }
        $product_temp_colours= DB::table('product_temp_colours')
            ->where(array('product_temp_id'=>$product_temp_id,'state'=>1))
            ->get()
            ->toArray();
        return $product_temp_colours;
    }
}